package com.huitone.gddw.controller.analysis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.CallTestPhone;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.analysis.ICallTestPhoneService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 拨测号码 控制器
 *
 */
@Controller
@RequestMapping(value="callTestPhone")
public class CallTestPhoneController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(CallTestPhoneController.class);
	
	@Autowired
	private ICallTestPhoneService callTestPhoneService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<CallTestPhone> query(QueryPage<CallTestPhone> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询拨测号码";
		Map<String, Object> params = new HashMap<String, Object>();
		String testPhone = request.getParameter("testPhone");
		String username = request.getParameter("username");
		String status = request.getParameter("status");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		params.put("testPhone", testPhone);
		params.put("username", username);
		params.put("status", status);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		page.setParams(params);
		QueryPage<CallTestPhone> result = null;
		try {
			result = callTestPhoneService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询拨测号码信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 更改状态
	 * @param recId 记录ID
	 * @param status 更改后状态
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="changeStatus")
	public Message changeStatus(String recId, String status) {
		try {
			callTestPhoneService.changeStatus(recId,status);
			return new Message(true, "操作成功！");
		} catch (Exception e) {
			LOGGER.error("更改拨测号码状态过程中出现异常：" + e.getMessage(), e);
			return new Message(false, "操作失败！");
		}
	}
	
	/**
	 * 修改页面数据准备
	 * @param recId 记录ID
	 * @return CallTestPhone
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public CallTestPhone toUpdate(String recId) {
		try {
			CallTestPhone entity = callTestPhoneService.toUpdate(recId);
			return entity;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new CallTestPhone();
		}
	}
	
	/**
	 * 保存
	 * @param entity CallTestPhone
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(CallTestPhone entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加拨测号码";
		boolean isSuccess = true;
		String msg;
		try {
			callTestPhoneService.save(entity);
			msg = "保存成功！";
		} catch (Exception e) {
			LOGGER.error("保存拨测号码信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "保存失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 更新
	 * @param entity CallTestPhone
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(CallTestPhone entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改拨测号码";
		boolean isSuccess = true;
		String msg;
		try {
			CallTestPhone callTestPhone = callTestPhoneService.selectByPrimaryKey(entity.getRecId());
			callTestPhone.setTestPhone(entity.getTestPhone());
			callTestPhone.setUsername(entity.getUsername());
			callTestPhone.setStatus(entity.getStatus());
			callTestPhoneService.update(callTestPhone);
			msg = "修改成功！";
		} catch (Exception e) {
			LOGGER.error("修改拨测号码信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 删除
	 * @param recId 记录ID
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String recId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除拨测号码";
		boolean isSuccess = true;
		String msg;
		try {
			callTestPhoneService.delete(recId);
			msg = "删除成功！";
		} catch (Exception e) {
			LOGGER.error("删除拨测号码信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "删除失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	@ResponseBody
	@RequestMapping(value="getTestPhone")
	public List<CallTestPhone> getCallTest(HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		for (Object name : request.getParameterMap().keySet()) {
			params.put(name.toString(), request.getParameter(name.toString()));
		}
		List<CallTestPhone> list = callTestPhoneService.getRecord(params);
		return list;
	}
	
}
